<template>
  <div>
    <a-layout id="components-layout-demo-side" style="min-height: 100vh">
      <a-layout-sider v-model="collapsed" collapsible class="leftNav">
        <div class="logo">
          <a-avatar :size="58" src="../../static/image/toux.gif" />
          <div v-if="!collapsed" class="name">动画视频系统</div>
        </div>
        <a-menu theme="dark" mode="inline" @openChange="openChange" @select="jump" :selectedKeys="[key]"
          :openKeys="sub">
          <a-menu-item key="4">
            <a-icon type="home" />
            <span>数据统计</span>
          </a-menu-item>
          <a-menu-item key="0">
            <a-icon type="home" />
            <span>作品管理</span>
          </a-menu-item>

          <a-menu-item key="3">
            <a-icon type="profile" />
            <span>系列管理</span>
          </a-menu-item>

          <a-menu-item key="5">
            <a-icon type="profile" />
            <span>账户信息</span>
          </a-menu-item>
          <!-- v-if="admin" -->
          <a-sub-menu key="sub3" v-if="admin">
            <span slot="title">
              <a-icon type="team" /><span>管理端</span>
            </span>

            <a-menu-item key="2">视频列表 </a-menu-item>
            <a-menu-item key="6">作者列表 </a-menu-item>
            <a-menu-item key="7">收益统计 </a-menu-item>
          </a-sub-menu>
        </a-menu>
      </a-layout-sider>
      <a-layout>
        <a-layout-header class="headerBox">
          <div class="headerT">{{ headerText }}</div>
          <div class="quit">
            <div class="welcome">欢迎您，{{ userUn.name }}</div>
            <a-button type="primary" ghost @click="quit"> 退出登录 </a-button>
          </div>
        </a-layout-header>
        <a-layout-content style="margin: 0 16px">
          <a-breadcrumb style="margin: 40px 0 10px 0">
            <a-breadcrumb-item v-for="(item, index) in routeList" :key="index">
              <span v-show="index == 0">{{ item.meta.title }}</span>
              <router-link :to="{ path: item.path }" v-show="index != 0">
                {{ item.meta.title }}
              </router-link>
            </a-breadcrumb-item>
          </a-breadcrumb>
          <div class="content">
            <a-spin tip="Loading..." size="large" class="loading" :spinning="spinning"></a-spin>
            <router-view />
          </div>
        </a-layout-content>
        <!-- <a-layout-footer style="text-align: center">
          Ant Design ©2018 Created by Ant UED
        </a-layout-footer> -->
      </a-layout>
    </a-layout>
  </div>
</template>

<script>
export default {
  name: "index",
  data () {
    return {
      collapsed: false,
      spinning: false,
      headerText: "",
      routeList: [],
      key: "",
      sub: [],
      userUn: {},
      quitConfirmLoading: false,
      quitVisible: false,

      crawler: [],
      admin: false
    }
  },
  created () {
    this.userUn = JSON.parse(localStorage.getItem("userInfo"))
    if (this.userUn.account == 'text123456') {
      this.admin = true
    }
    var arr = [
      { path: "/index", name: "index", meta: { title: "首页" } },
      {
        path: "/index/statistics",
        name: "statistics",
        meta: { title: "数据统计" },
      },
    ]
    //防刷新
    this.routeList = JSON.parse(localStorage.getItem("routeList")) || arr
    this.key = localStorage.getItem("key") || "4"
    this.headerText =
      localStorage.getItem("headerText") || "作品管理 Data List"
    this.sub = JSON.parse(localStorage.getItem("sub"))

  },
  //监听路由变化
  watch: {
    $route (to, from) {
      var matched = to.matched //this.$route.matched
      //过滤出有用信息 以便存入缓存
      var list = []
      matched.map((item) => {
        if (item.name !== "index") {
          var arr = {
            path: item.path,
            name: item.name,
            meta: item.meta,
          }
          list.push(arr)
        }
      })
      //这个地方是为了固定第一级目录是首页不变
      matched = [
        { path: "/index", name: "index", meta: { title: "首页" } },
        ...list,
      ]
      // console.log(matched);
      this.routeList = matched
      localStorage.setItem("routeList", JSON.stringify(matched))

      this.key = localStorage.getItem("key") || "4"
      this.headerText =
        localStorage.getItem("headerText") || "作品管理 Data List"
      this.sub = JSON.parse(localStorage.getItem("sub"))
    },
  },
  methods: {
    //跳转到账户信息界面
    toUserInfo () {
      this.$router.push({ path: "/userInfo/userInfo" })
    },
    openChange (e) {
      // console.log(e);
      this.sub = e
      localStorage.setItem("sub", JSON.stringify(e))
    },
    jump (e) {
      console.log(e.key)
      this.key = e.key
      switch (e.key) {
        case "0":
          this.$router.push({ path: "/index/souyeMsg" })
          this.headerText = "作品管理 Home Recommend"
          break
        case "1":
          this.$router.push({ path: "/index/userMsg" })
          this.headerText = "用户管理 User Manage"
          break
        case "2":
          this.$router.push({ path: "/index/authorMsg" })
          this.headerText = "视频管理 author authorMsg"
          break
        case "3":
          this.$router.push({ path: "/index/seriesMsg" })
          this.headerText = "系列管理 series Manage"
          break
        case "4":
          this.$router.push({ path: "/index/statistics" })
          this.headerText = "数据统计 statistics Manage"
          break

        case "5":
          this.$router.push({ path: "/index/userInfo" })
          this.headerText = "账户信息 userInfo Manage"
          break
        case "6":
          this.$router.push({ path: "/index/userMsg" })
          this.headerText = "作者列表 userMsg Manage"
          break
        case "7":
          this.$router.push({ path: "/index/userType" })
          this.headerText = "收益数据 userType Manage"
          break


      }
      localStorage.setItem("headerText", this.headerText)
      localStorage.setItem("key", e.key)
    },
    quit () {
      let that = this
      that.$confirm({
        title: "您确定要退出吗？",
        onOk () {
          return new Promise((resolve, reject) => {
            setTimeout(() => {
              setTimeout(Math.random() > 0.5 ? resolve : reject, 50)
              // window.location = "/admin-vue/dist/index.html"
              that.$router.replace({ path: "/" })
              localStorage.removeItem("key")
              localStorage.removeItem("userInfo")
              localStorage.removeItem("headerText")
              localStorage.removeItem("routeList")
              localStorage.removeItem("sub")
              localStorage.removeItem("vueXStore")
            }, 1000)
          }).catch(() => console.log("Oops errors!"))
        },
        onCancel () { },
      })
    },
  },
}
</script>

<style scoped>
.logo {
  margin: 16px;
  text-align: center;
  color: #fff;
}

.name {
  font-size: 15px;
  margin-top: 10px;
}

.headerBox {
  background: #fff;
  padding-left: 10px;
  padding-right: 40px;
  display: flex;
  justify-content: space-between;
}

.headerT {
  font-size: 18px;
  font-weight: bold;
}

.content {
  padding: 24px;
  background: #fff;
  height: 85vh;
  overflow-y: auto;
}

.content::-webkit-scrollbar {
  display: none;
}

.loading {
  position: absolute;
  top: 50%;
  left: 50%;
}

.quit {
  display: flex;
  align-items: center;
}

.welcome {
  margin-right: 30px;
  font-size: 14px;
}

.leftNav {
  overflow-y: auto;
  height: 100vh;
}

.leftNav::-webkit-scrollbar {
  display: none;
}
</style>